package lab;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import widgets.ChooseRandom;
import widgets.ChooseData;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import process.Dispatcher;
import widgets.Diagram;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JTextArea;
//import stat.DiscretHisto;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import widgets.experiments.ExperimentControl;
import widgets.regres.RegresAnaliser;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollBar;
import widgets.trans.TransMonitor;
import widgets.trans.TransMonitorView;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.CardLayout;
import paint.DiagramPanel;
import qusystem.IModelFactory;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class mainGUI {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainGUI window = new mainGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public mainGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	
	private ChooseRandom timeRandom;
	private ChooseRandom timeKranRandom;
	
	public ChooseRandom getTimeKranRandom() {
		return timeKranRandom;
	}

	public ChooseRandom getTimeGruzovikRandom() {
		return timeGruzovikRandom;
	}

	public ChooseData getSizeOfSquare() {
		return sizeOfSquare;
	}

	public ChooseData getCountOfGruz() {
		return countOfGruz;
	}

	public ChooseData getTimeWork() {
		return timeWork;
	}
	
	

	private ChooseRandom timeGruzovikRandom;
	private ChooseData sizeOfSquare;
	private ChooseData countOfGruz;
	private ChooseData timeWork;
	private ChooseData countOfShips;

	
	private JRadioButton radioT;
	private JRadioButton radioN;
	private JRadioButton radioPP1;
	private JRadioButton radioPP2;
	
	public JRadioButton getRadioT(){
		return radioT;
	}
	
	public JRadioButton getRadioN(){
		return radioN;
	}
	public JRadioButton getRadioPP1() {
		return radioPP1;
	}

	public void setRadioPP1(JRadioButton radioPP1) {
		this.radioPP1 = radioPP1;
	}
	public JRadioButton getRadioPP2() {
		return radioPP2;
	}

	public void setRadioPP2(JRadioButton radioPP2) {
		this.radioPP2 = radioPP2;
	}
	
	private Diagram diagramRegres;
	private Diagram diagramPP;
	
	public ChooseData getCountOfShips() {
		return countOfShips;
	}

	public ChooseRandom getTimeRandom(){
		return timeRandom;
	}
	
	private Diagram container;
	private Diagram gruzovikToKran;
	private Diagram gruzovikOnRoad;
	private Diagram statDiagram;
	private Diagram statDiagram2;
	private Diagram squareDiagram;
	private JTextArea stat2TextArea;
	
	
	public Diagram getSquareDiagram() {
		return squareDiagram;
	}

	private JTextArea textArea;
	
	public JTextArea getTextArea() {
		return textArea;
	}

	public Diagram getStatDiagram() {
		return statDiagram;
	}

	private JButton butStart;
	private JButton butStat;
	private JButton but2Stat;
	/**
	 * @wbp.nonvisual location=84,699
	 */
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup1 = new ButtonGroup();
	private Factory factory;
	/**
	 * @wbp.nonvisual location=170,571
	 */
	//private final DiscretHisto discretHisto = new DiscretHisto();
	
	public JButton getButStat() {
		return butStat;
	}

	public JButton getButStart() {
		return butStart;
	}

	public Diagram getContainer() {
		return container;
	}

	public Diagram getGruzovikToKran() {
		return gruzovikToKran;
	}

	public Diagram getGruzovikOnRoad() {
		return gruzovikOnRoad;
	}
	
	
	
	private void startTest() {
		getContainer().clear();
		getGruzovikToKran().clear();
		getGruzovikOnRoad().clear();
		getSquareDiagram().clear();
		Factory factory = new Factory(mainGUI.this);
		final Dispatcher dispatcher = new Dispatcher();
		Model model = (Model) factory.createModel(dispatcher);
		model.initForTest();
		dispatcher.start();
		getButStart().setEnabled(false);
		new Thread() {
			public void run() {	
				try {
					dispatcher.getThread().join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getButStart().setEnabled(true);
			}
		}.start();
	}
	
	private void startStat() {
		getStatDiagram().clear();
		getTextArea().setText("");
		Factory factory = new Factory(mainGUI.this);
		final Dispatcher dispatcher = new Dispatcher();
		final Model model = (Model) factory.createModel(dispatcher);
		model.initForStat();
		dispatcher.start();
		getButStat().setEnabled(false);
		new Thread() {
			public void run() {
				try {
					dispatcher.getThread().join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getButStat().setEnabled(true);
				model.getqGruzovikToKran().showRelFrec(getStatDiagram());
				getTextArea().setText(
						model.getqGruzovikToKran().toString());
			}
		}.start();
	}
	
	private void startStat2(){
		getStatDiagram2().clear();
		getStat2TextArea().setText("");
		Factory factory = new Factory(mainGUI.this);
		final Dispatcher dispatcher = new Dispatcher();
		final Model model = (Model) factory.createModel(dispatcher);
		model.initForStat();
		dispatcher.start();
		getBut2Stat().setEnabled(false);
		new Thread() {
			public void run() {
				try {
					dispatcher.getThread().join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getBut2Stat().setEnabled(true);
				//model.getqGruzovikToKran().showRelFrec(getStatDiagram());
				model.getHistoSq().showRelFrec(getStatDiagram2());
				getStat2TextArea().setText(model.getHistoSq().toString());
			}
		}.start();
	}



	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 631, 817);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label = new JLabel("\u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u044B");
		label.setBounds(10, 11, 123, 14);
		label.setFont(new Font("Arial", Font.BOLD, 14));
		
		ChooseRandom chooseRandom_1 = new ChooseRandom();
		chooseRandom_1.setBounds(10, 139, 278, 42);
		timeKranRandom = chooseRandom_1;
		
		
		JLabel label_2 = new JLabel("\u0412\u0440\u0435\u043C\u044F, \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043D\u043D\u043E\u0435 \u043A\u0440\u0430\u043D\u043E\u043C");
		label_2.setBounds(10, 114, 171, 14);
		
		JLabel label_3 = new JLabel("\u0412\u0440\u0435\u043C\u044F, \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043D\u043D\u043E\u0435 \u0433\u0440\u0443\u0437\u043E\u0432\u0438\u043A\u043E\u043C");
		label_3.setBounds(10, 192, 220, 14);
		
		ChooseRandom chooseRandom_2 = new ChooseRandom();
		chooseRandom_2.setBounds(10, 217, 278, 42);
		timeGruzovikRandom = chooseRandom_2;
		
		
		final ChooseData chooseData = new ChooseData();
		
		chooseData.setBounds(10, 270, 278, 53);
		chooseData.setFont(new Font("Dialog", Font.PLAIN, 12));
		chooseData.setTitle("\u0420\u0430\u0437\u043C\u0435\u0440 \u043F\u043B\u043E\u0449\u0430\u0434\u043A\u0438");
		chooseData.setText("10");
		
		sizeOfSquare = chooseData;
		
		final ChooseData chooseData_2 = new ChooseData();
	
		chooseData_2.setBounds(10, 335, 278, 54);
		chooseData_2.setTitle("\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u0433\u0440\u0443\u0437\u043E\u0432\u0438\u043A\u043E\u0432");
		chooseData_2.setText("10");
		
		countOfGruz = chooseData_2;
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(307, 7, 308, 710);
		//tabbedPane.addTab("ololo", null);
		
		JPanel panel = new JPanel();
		panel.setToolTipText("");
		tabbedPane.addTab("Test", null, panel, null);
		panel.setLayout(null);
		
		Diagram diagram = new Diagram();
		diagram.setTitleText("\u041A\u043E\u043D\u0442\u0435\u0439\u043D\u0435\u0440\u044B \u0434\u043B\u044F \u0440\u0430\u0437\u0433\u0440\u0443\u0437\u043A\u0438");
		diagram.setBounds(10, 11, 300, 151);
		panel.add(diagram);
		diagram.setPainterColor(Color.BLACK);
		
		container =diagram;
		
		Diagram diagram_1 = new Diagram();
		diagram_1.setTitleText("\u041E\u0447\u0435\u0440\u0435\u0434\u044C \u0433\u0440\u0443\u0437\u043E\u0432\u0438\u043A\u043E\u0432 \u043A \u043A\u0440\u0430\u043D\u0443");
		diagram_1.setBounds(10, 344, 300, 151);
		panel.add(diagram_1);
		diagram_1.setPainterColor(Color.BLACK);
		
		gruzovikToKran = diagram_1;
		
		Diagram diagram_2 = new Diagram();
		diagram_2.setTitleText("\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u0433\u0440\u0443\u0437\u043E\u0432\u0438\u043A\u043E\u0432 \u0432 \u0434\u043E\u0440\u043E\u0433\u0435");
		diagram_2.setBounds(10, 506, 300, 136);
		panel.add(diagram_2);
		diagram_2.setPainterColor(Color.BLACK);
		
		gruzovikOnRoad = diagram_2;
		
		JButton btnNewButton = new JButton("\u0421\u0434\u0435\u043B\u0430\u0442\u044C \u0442\u0430\u043A, \u0447\u0442\u043E \u0431 \u0440\u0430\u0431\u043E\u0442\u0430\u043B\u043E");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startTest();
			}
		});
		btnNewButton.setBounds(12, 637, 279, 33);
		panel.add(btnNewButton);
		butStart = btnNewButton;
		
		ChooseRandom chooseRandom_3 = new ChooseRandom();
		chooseRandom_3.setBounds(10, 61, 278, 42);
		timeRandom = chooseRandom_3;
		
		JLabel label_4 = new JLabel("\u0412\u0440\u0435\u043C\u044F \u0434\u043E \u0441\u043B\u0435\u0434\u0443\u044E\u0449\u0435\u0433\u043E \u043F\u0440\u0438\u0431\u044B\u0442\u0438\u044F");
		label_4.setBounds(10, 36, 278, 14);
		
		final ChooseData chooseData_3 = new ChooseData();
	
		chooseData_3.setBounds(10, 400, 278, 54);
		chooseData_3.setTitle("\u0412\u0440\u0435\u043C\u044F \u0440\u0430\u0431\u043E\u0442\u044B");
		
		timeWork = chooseData_3;
		
		frame.getContentPane().setLayout(null);
		frame.getContentPane().add(label);
		frame.getContentPane().add(chooseRandom_1);
		frame.getContentPane().add(label_2);
		frame.getContentPane().add(label_3);
		frame.getContentPane().add(chooseRandom_2);
		frame.getContentPane().add(chooseData);
		frame.getContentPane().add(chooseData_2);
		frame.getContentPane().add(tabbedPane);
		frame.getContentPane().add(chooseRandom_3);
		frame.getContentPane().add(label_4);
		frame.getContentPane().add(chooseData_3);
		
		chooseData_2.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				
				getGruzovikToKran().setVerticalMaxText(Integer.toString(Integer.parseInt(chooseData_2.getText())+10));
				getGruzovikOnRoad().setVerticalMaxText(Integer.toString(Integer.parseInt(chooseData_2.getText())+10));
			}
		});
		getGruzovikOnRoad().setVerticalMaxText("20");
		getGruzovikToKran().setVerticalMaxText("20");
		getGruzovikOnRoad().setVerticalMinText("-1");
		chooseData_3.setText("100");
		getGruzovikOnRoad().setHorizontalMaxText(chooseData_3.getText());
		getGruzovikToKran().setHorizontalMaxText(chooseData_3.getText());
		getContainer().setHorizontalMaxText(chooseData_3.getText());
		
		final ChooseData chooseData_1 = new ChooseData();
		
		chooseData_1.setTitle("\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u043A\u043E\u043D\u0442\u0435\u0439\u043D\u0435\u0440\u043E\u0432");
		chooseData_1.setText("20");
		chooseData_1.setBounds(10, 465, 278, 54);
		frame.getContentPane().add(chooseData_1);
		countOfShips = chooseData_1;
		getContainer().setVerticalMaxText("30");
		
		Diagram diagram_4 = new Diagram();
		diagram_4.setTitleText("\u0417\u0430\u0433\u0440\u0443\u0436\u0435\u043D\u043D\u043E\u0441\u0442\u044C \u043F\u043B\u043E\u0449\u0430\u0434\u043A\u0438");
		diagram_4.setBounds(10, 173, 300, 160);
		panel.add(diagram_4);
		squareDiagram = diagram_4;
		diagram_4.setVerticalMaxText("20");
		diagram_4.setHorizontalMaxText("100");
		diagram_4.setPainterColor(Color.BLACK);
		
		JPanel panel_4 = new JPanel();
		tabbedPane.addTab("PerProc", null, panel_4, null);
		panel_4.setLayout(null);
		
		Diagram diagram_7 = new Diagram();
		diagram_7.setVerticalMaxText("10");
		diagram_7.getDiagramPanel().setGridByY(10);
		diagram_7.setPainterColor(Color.BLUE);
		diagram_7.setBounds(30, 245, 265, 211);
		diagram_7.setTitleText("zagruzhenost' ploschadki");
		panel_4.add(diagram_7);
		diagramPP=diagram_7; 
		
		TransMonitorView transMonitorView = new TransMonitorView();
		transMonitorView.setПараллельно_моделей("100");
		transMonitorView.setКоличество_интервалов("30");
		transMonitorView.setИнтервал_усреднения("1");
		transMonitorView.setBounds(53, 12, 205, 221);
		transMonitorView.setFactory(getFactory());
		transMonitorView.setDiagram(diagram_7);
		panel_4.add(transMonitorView);
				
		Diagram diagram_3 = new Diagram();
		diagram_3.setTitleText("Ochered k krany");
		diagram_3.setBounds(10, 0, 287, 197);
		statDiagram = diagram_3;
		diagram_3.setPainterColor(Color.BLACK);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Statistic", null, panel_1, null);
		panel_1.setLayout(null);
		panel_1.add(diagram_3);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 208, 287, 305);
		panel_1.add(scrollPane);
		
		JTextArea textArea_1 = new JTextArea();
		textArea_1.setEditable(false);
		scrollPane.setViewportView(textArea_1);
		this.textArea = textArea_1;
		
		JButton btnNewButton_1 = new JButton("\u041A\u043B\u0430\u0446 \u043A\u043B\u0430\u0446");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startStat();
			}
		});
		butStat = btnNewButton_1;
		btnNewButton_1.setBounds(10, 524, 287, 23);
		panel_1.add(btnNewButton_1);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("Stat2", null, panel_3, null);
		panel_3.setLayout(null);
		
		Diagram diagram_6 = new Diagram();
		diagram_6.setTitleText("Zagrugenost ploschadki");
		diagram_6.setBounds(12, 12, 279, 197);
		panel_3.add(diagram_6);
		statDiagram2=diagram_6;
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(12, 221, 279, 317);
		panel_3.add(scrollPane_1);
		
		JTextArea textArea_2 = new JTextArea();
		textArea_2.setEditable(false);
		scrollPane_1.setViewportView(textArea_2);
		stat2TextArea=textArea_2;
		
		JButton btnNewButton_2 = new JButton("test");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startStat2();
			}
		});
		but2Stat=btnNewButton_2;
		btnNewButton_2.setBounds(12, 550, 279, 25);
		panel_3.add(btnNewButton_2);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Regres", null, panel_2, null);
		panel_2.setLayout(null);
		
		ExperimentControl experimentControl = new ExperimentControl();
		experimentControl.setTextFactors(" 1 2 3 4 5 6 7 8 9");
		experimentControl.setBounds(12, 12, 282, 148);
		panel_2.add(experimentControl);
		experimentControl.setFactory(getFactory());
		experimentControl.setDiagram(diagramRegres);
		
		Diagram diagram_5 = new Diagram();
		diagram_5.setVerticalMaxText("10");
		diagram_5.setHorizontalMaxText("10");
		diagram_5.setTitleText("t ogud gruzov ot  N");
		diagram_5.setBounds(22, 172, 272, 197);
		panel_2.add(diagram_5);
		experimentControl.setDiagram(diagram_5);
		experimentControl.setFactory(new Factory(this));
		diagramRegres=diagram_5;
		
		RegresAnaliser regresAnaliser = new RegresAnaliser();
		regresAnaliser.setBounds(12, 381, 282, 224);
		panel_2.add(regresAnaliser);
		regresAnaliser.setIRegresable(experimentControl);
		regresAnaliser.setDiagram(diagram_5);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("t ogud gruzov ot   N.");
		rdbtnNewRadioButton.setFont(new Font("Arial", Font.BOLD, 12));
		rdbtnNewRadioButton.setBounds(12, 613, 282, 23);
		panel_2.add(rdbtnNewRadioButton);
		radioT=rdbtnNewRadioButton;
		rdbtnNewRadioButton.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				if (mainGUI.this.radioT.isSelected()){
					mainGUI.this.diagramRegres.setTitleText("t ogud gruzov ot  N.");
				}
				
			}
		});
		
		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("avg zagrugenost ploschedi ot N gruzovika");
		rdbtnNewRadioButton_1.setFont(new Font("Arial", Font.BOLD, 12));
		rdbtnNewRadioButton_1.setBounds(12, 640, 282, 23);
		panel_2.add(rdbtnNewRadioButton_1);
		radioN=rdbtnNewRadioButton_1;
		rdbtnNewRadioButton_1.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if (mainGUI.this.radioN.isSelected()){
					mainGUI.this.diagramRegres.setTitleText("avg zagrugenost ploschedi ot N gruzovika");
			}
			}
		});
		
		buttonGroup.add(radioT);
		buttonGroup.add(radioN);
		radioT.setSelected(true);
		
		chooseData_3.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				getGruzovikOnRoad().setHorizontalMaxText(chooseData_3.getText());
				getGruzovikToKran().setHorizontalMaxText(chooseData_3.getText());
				getContainer().setHorizontalMaxText(chooseData_3.getText());
				getSquareDiagram().setHorizontalMaxText(chooseData_3.getText());
			}
		});
		chooseData_1.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				getContainer().setVerticalMaxText(Integer.toString(Integer.parseInt(chooseData_1.getText())+10));
			}
		});
		chooseData.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				getSquareDiagram().setVerticalMaxText(Integer.toString(Integer.parseInt(chooseData.getText())+10));
			}
		});
	}

	private Factory getFactory() {
		if (factory == null) {
			factory = new Factory(this);
		}
		return factory;
	}

	public Diagram getStatDiagram2() {
		return statDiagram2;
	}

	public void setStatDiagram2(Diagram statDiagram2) {
		this.statDiagram2 = statDiagram2;
	}

	public JTextArea getStat2TextArea() {
		return stat2TextArea;
	}

	public void setStat2TextArea(JTextArea stat2TextArea) {
		this.stat2TextArea = stat2TextArea;
	}

	public JButton getBut2Stat() {
		return but2Stat;
	}

	public void setBut2Stat(JButton but2Stat) {
		this.but2Stat = but2Stat;
	}
}
